package org.jeecg.codegeneweb.template;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.Data;
import org.jeecg.codegeneweb.DestinationLocation;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


//@Data
public class TemplateLocaltion {
    public TemplateLocaltion(String templatePath) {
        this.templatePath = templatePath;
    }
    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }

    public void setTemplateSingleFileDomains(List<TemplateSingleFileDomain> templateSingleFileDomains) {
        this.templateSingleFileDomains = templateSingleFileDomains;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    private String templatePath;//"D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\default\\one\\java";
    private List<TemplateSingleFileDomain> templateSingleFileDomains=new ArrayList<>();

    public File getTemplateFile() throws Exception {
        return new File(templatePath);
    }

    public List<TemplateSingleFileDomain> getTemplateSingleFileDomains() throws Exception{

        List<File> files = getFileList(getTemplateFile());
        List<TemplateSingleFileDomain> singleFileDomainList=new ArrayList<>();
        for(File file:files){
            TemplateSingleFileDomain templateSingleFileDomain=new TemplateSingleFileDomain();
            templateSingleFileDomain.setTemplateLocaltion(this);
            templateSingleFileDomain.setTemplatefile(file);
            templateSingleFileDomain.setTemplatefilename(templateSingleFileDomain.getTemplateLocaltion().getTemplatePath());
            if(file.isDirectory()){
                templateSingleFileDomain.setDir(true);
            }else {
                templateSingleFileDomain.setDir(false);
            }

            String filename=file.getAbsolutePath();
            templateSingleFileDomain.setOriginalOnlyFileName(file.getName());
            templateSingleFileDomain.setOriginalAbsolutePath(file.getAbsolutePath());
            templateSingleFileDomain.setOriginalRelativePath(file.getAbsolutePath().substring(templatePath.length()));
            //templateSingleFileDomain.seto
            if (file.getName().endsWith("javai") || file.getName().endsWith("vuei")) {

                filename=filename.substring(0, filename.length() - 1);
            }
            DestinationSingleFileDomain dfd=new DestinationSingleFileDomain(templateSingleFileDomain);
            dfd.setTemplateSingleFileDomain(templateSingleFileDomain);
            templateSingleFileDomain.setDestinationSingleFileDomain(dfd);
            singleFileDomainList.add(templateSingleFileDomain);
        }
        return singleFileDomainList;
    }
    public List<TemplateSingleFileDomain> getTemplateSingleFileDomainsextext(DestinationLocation dt) throws Exception{

        List<File> files = getFileList(getTemplateFile());
        List<TemplateSingleFileDomain> singleFileDomainList=new ArrayList<>();
        for(File file:files){
            TemplateSingleFileDomain templateSingleFileDomain=new TemplateSingleFileDomain();
            templateSingleFileDomain.setTemplateLocaltion(this);
            templateSingleFileDomain.setTemplatefile(file);
            templateSingleFileDomain.setTemplatefilename(templateSingleFileDomain.getTemplateLocaltion().getTemplatePath());
            if(file.isDirectory()){
                templateSingleFileDomain.setDir(true);
            }else {
                templateSingleFileDomain.setDir(false);
            }

            String filename=file.getAbsolutePath();
            System.out.println("___filename:"+filename);
            templateSingleFileDomain.setOriginalOnlyFileName(file.getName());
            templateSingleFileDomain.setOriginalAbsolutePath(file.getAbsolutePath());
            templateSingleFileDomain.setOriginalRelativePath(file.getAbsolutePath().substring(templatePath.length()));
            //templateSingleFileDomain.seto
            if (file.getName().endsWith("javai") || file.getName().endsWith("vuei")) {

                filename=filename.substring(0, filename.length() - 1);
            }
            DestinationSingleFileDomain dfd=new DestinationSingleFileDomain(templateSingleFileDomain);
            dfd.setEntityname(dt.getEntityname());
            dfd.setEntitypackage(dt.getEntitypackage());
            dfd.setTemplateSingleFileDomain(templateSingleFileDomain);
            templateSingleFileDomain.setDestinationSingleFileDomain(dfd);
            singleFileDomainList.add(templateSingleFileDomain);
        }
        return singleFileDomainList;
    }
    public List<String> replace模板中的文件名及变量(List<File> files, Map<String, Object> map, DestinationLocation destinationLocation) {
        List<String> filenamelist = new ArrayList<>();
        for (File file : files) {
            //如果后缀是javai，那么去掉i
            String filename=file.getAbsolutePath();
            if (file.getName().endsWith("javai") || file.getName().endsWith("vuei")) {

                filename=filename.substring(0, filename.length() - 1);
            }
            filenamelist.add(filename);
        }
        List<String> afterfilenamelist = replace模版中的变量(filenamelist, map);
        //将模板中的文件名截取后部分与 目标地址的前部分进行合并
        List<String>filenamefinal=destinationLocation.combinefilenameext(afterfilenamelist);
        return afterfilenamelist;
    }

    public List<String> replace模版中的变量(List<String> filenamelist, Map<String, Object> map) {
        List<String> afterfilenamelist = new ArrayList<>();
        //对文件名的变量进行替换
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        for (String filename : filenamelist) {
            StringWriter stringWriter = new StringWriter();

            try {
                Template template = new Template("templateString...", new StringReader(filename), configuration);
                template.process(map, stringWriter);
                //return stringWriter.toString();
                afterfilenamelist.add(stringWriter.toString());

            } catch (Exception ex) {
                throw new IllegalStateException("cannot process templateString:" + filename + " cause:" + ex, ex);
            }
        }
        return afterfilenamelist;
    }
    private List<File> getFileList(File folder) {
        List<File> list = new ArrayList<File>();
        File[] files = folder.listFiles();
        for (File file : files) {
            list.add(file);
            if (file.isDirectory()) {
                List<File> fileList = getFileList(file);
                list.addAll(fileList);
            }
        }
        return list;
    }
    public static void main(String[] args)  {
        try {
            new File("d:\\tmp\\one\\two\\three").mkdirs();
            new File("d:\\tmp\\one\\two\\four").mkdirs();
            new File("d:\\tmp\\one\\tw1").mkdirs();
            File file=new File("d:\\tmp\\one\\two\\three\\one.txt");
            file.createNewFile();
            System.out.println("file.getAbsolutePath()"+file.getAbsolutePath());
            System.out.println("file.getName()"+file.getName());
            System.out.println("file.getParentFile().getAbsolutePath()"+file.getParentFile().getAbsolutePath());
            System.out.println("file.getPath()"+file.getPath());
            System.out.println("file.getCanonicalPath()"+file.getCanonicalPath());
            String templatepath="d:\\tmp\\one\\two";
            String filename=file.getAbsolutePath().substring(templatepath.length(), file.getAbsolutePath().length());
            System.out.println(filename);

        }catch (Exception ex){

        }


    }
}
